package com.example.search.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.SimpleVideoEntity

import com.example.image.ImgOptions
import com.example.image.LoadImageUtil
import com.example.search.databinding.ItemSearchLayoutBinding
import com.luck.picture.lib.interfaces.OnItemClickListener
import kotlin.math.max

class SearchVideoListAdapter constructor(private val list: MutableList<SimpleVideoEntity>):RecyclerView.Adapter<SearchVideoListAdapter.ItemViewHolder>(){

    private var scollSate = RecyclerView.SCROLL_STATE_IDLE//滚动停止
    private var isReloading = false//是否正在预加载中
    var preloadData:(()->Unit)?=null//预加载执行方法
    var preloadEndCount = 0 //滚动倒数第几个Item时候触发预加载
    private var itemClickListener:OnItemClickListener?=null

    inner class ItemViewHolder(val dataBinding:ItemSearchLayoutBinding):RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemDataBinding = ItemSearchLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ItemViewHolder(itemDataBinding)
    }

    override fun getItemCount(): Int =list.size
    interface OnItemClickListener{
        fun onItemClick(v:View,position:Int)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        //自己定义一个entity,参数已经通过意图成功有数据了
        val entity = list[position]

        //设置标题和描述
        holder.dataBinding.tvTitle.text = entity.title
        holder.dataBinding.tvDesc.text = entity.description
        //设置作者头像
        LoadImageUtil.loadUrl(holder.dataBinding.root.context,
            entity.avatar_url,
            holder.dataBinding.ivAvatar,
            ImgOptions.Builder.isCircle(true).build()
            )
        //设置封面图
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.videomainimag,
            holder.dataBinding.ivVideoThumbnail,
            ImgOptions.Builder.isCircle(false).roundRadius(10).build()
        )
        //设置点击事件
        holder.dataBinding.ivVideoPlay.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }
        holder.dataBinding.ivCommend.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }
        holder.dataBinding.ivMore.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }

        //检查是否执行预加载更多数据
        checkPreload(position)
    }
    //给滑动的值赋值
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object:RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                scollSate = newState
            }
        })

    }

    override fun getItemViewType(position: Int): Int {
        return 0
    }
    private fun checkPreload(position: Int){
        //newState == RecyclerView.SCROLL_STATE_IDLE
        if(scollSate != RecyclerView.SCROLL_STATE_IDLE && position == max(list.size-1 -preloadEndCount,0)
            &&!isReloading//是否处于加载中
            ){
            isReloading = true
            //执行预加载方法
            preloadData?.invoke()
        }else{
            isReloading = false
        }
    }




}